package com.longcai.cm.moudules.system.controller;

import com.alibaba.fastjson.JSON;
import com.longcai.cm.base.aop.Log;
import com.longcai.cm.base.controller.BaseController;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.StringUtils;
import com.longcai.cm.moudules.system.bean.TreeConfigBean;
import com.longcai.cm.moudules.system.domain.SysDept;
import com.longcai.cm.moudules.system.service.SysDeptService;
import com.longcai.cm.moudules.system.utils.CmTreeUtils;
import com.longcai.cm.utils.DateUtils;
import com.longcai.cm.moudules.system.utils.UUID;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysDept")
@Api(value = "系统管理-组织机构", tags = "系统管理-组织机构")
@ApiSort(3)
public class SysDeptController extends BaseController {


    @Resource
    private SysDeptService sysDeptService;

    /**
     * 【API】---- 获取所有组织机构列表
     *
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return R
     */

    @PostMapping("/getList")
    @ResponseBody
    @ApiOperation(value = "【S3003】获取机构列表", notes = "获取机构列表")
    @ApiOperationSupport(order = 3)
    @Log(value = "获取机构列表", type = "1")
    public R getList(HttpServletRequest request, HttpServletResponse response) {

        List<SysDept> allDept = sysDeptService.list(null);

        List<Map<String, Object>> allDeptMapList = JSON.parseObject(JSON.toJSONString(allDept), List.class);
        for (Map<String, Object> map : allDeptMapList) {
            map.put("title", map.get("deptName"));
            map.put("spread", true);
        }

        TreeConfigBean treeConfigBean = new TreeConfigBean();
        treeConfigBean.setPrimaryKey("deptId");
        treeConfigBean.setParentKey("parentId");
        List<Map<String, Object>> resultMap = CmTreeUtils.getTreeList(allDeptMapList, treeConfigBean);
        return R.success(resultMap);
    }


    /**
     * 【API】  ---- 保存部门信息
     *
     * @param sysDept  组织机构实体
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return R
     */
    @PostMapping("/save")
    @ResponseBody
    @ApiOperation(value = "【S3006】保存组织机构信息", notes = "保存组织机构信息")
    @ApiOperationSupport(order = 6)
    @Log(value = "保存组织机构信息", type = "1")
    public R savePC(SysDept sysDept, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isNotEmpty(sysDept.getDeptId())) {
            sysDept.setCreateTime(DateUtils.getTimeNow());
            sysDept.setDelFlag("0");
            sysDeptService.updateById(sysDept);
        } else {
            sysDept.setDeptId(UUID.get());
            sysDept.setCreateTime(DateUtils.getTimeNow());
            sysDept.setDelFlag("0");
            sysDeptService.save(sysDept);
        }
        return R.success();
    }


    /**
     * 【API】 ---- 删除组织机构数据
     *
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return R
     */
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "【S3009】删除组织机构", notes = "删除组织机构")
    @ApiOperationSupport(order = 9)
    @Log(value = "删除组织机构", type = "1")
    public R delete(HttpServletRequest request, HttpServletResponse response) {

        String ids = request.getParameter("ids");
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            //删除组织机构信息
            sysDeptService.removeById(idString);
        }

        return R.success();
    }


}